package cc.admin.modules.sys.controller;

import cc.admin.common.api.vo.Result;
import cc.admin.common.sys.vo.LoginUser;
import cc.admin.common.util.PasswordUtil;
import cc.admin.modules.sys.entity.SysUser;
import cc.admin.modules.sys.entity.SysUserSign;
import cc.admin.modules.sys.service.ISysUserService;
import cc.admin.modules.sys.service.ISysUserSignService;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Sys user sign
 *
 * @author 马雪辰
 * @date 2022.08.30 14:35:52
 * @description description
 */
@Slf4j
@RestController
@RequestMapping("/sys/usersign")
public class SysUserSignController {

	@Autowired
	private ISysUserSignService sysUserSignService;
	@Autowired
	private ISysUserService sysUserService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result<IPage<SysUserSign>> queryPageList(SysUserSign userSign, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
													@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
		Result<IPage<SysUserSign>> result = new Result<IPage<SysUserSign>>();
		QueryWrapper<SysUserSign> queryWrapper = new QueryWrapper<>();
		if (StrUtil.isNotEmpty(userSign.getUserIdText())) {
			queryWrapper.like("user_id_text", userSign.getUserIdText());
		}
		Page<SysUserSign> page = new Page<>(pageNo, pageSize);
		IPage<SysUserSign> pageList = sysUserSignService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		log.info(pageList.toString());
		return result;
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Result<?> add(@RequestBody SysUserSign sysUserSign) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		SysUserSign sign = sysUserSignService.getOne(new QueryWrapper<SysUserSign>().eq("user_id", sysUserSign.getUserId()));
		Result<SysUserSign> result = new Result<>();
		if (sign == null) {
			//设置创建时间
			sysUserSign.setCreateTime(new Date());
			sysUserSign.setCreateBy(loginUser.getId());
			sysUserSignService.save(sysUserSign);
			return Result.ok((Object) "添加成功！");
		} else {
			result.error500("不能重复添加");
			return result;
		}
	}

	@RequestMapping(value = "/edit", method = RequestMethod.PUT)
	public Result<?> edit(@RequestBody SysUserSign sysUserSign) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		Result<SysUserSign> result = new Result<>();
		//设置创建时间
		sysUserSign.setUpdateTime(new Date());
		sysUserSign.setUpdateBy(loginUser.getId());
		sysUserSignService.updateById(sysUserSign);
		return Result.ok((Object) "修改成功！");
	}

	/**
	 * 删除用户
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		this.sysUserSignService.removeById(id);
		return Result.ok("删除用户成功");
	}

	/**
	 * 获取签字
	 * Query sign text result
	 *
	 * @param password password
	 * @return the result
	 */
	@PostMapping("/querySignText")
	public Result<?> querySignText(String password) {
		String signText = sysUserSignService.querySignText(password);
		if (signText != null) {
			return Result.ok((Object) signText);
		} else {
			return Result.error("签字密码错误!");
		}
	}

	/**
	 * 判断是否存在签字信息
	 * Query sign by user id result
	 *
	 * @return the result
	 */
	@PostMapping("/querySignByUserId")
	public Result<?> querySignByUserId() {
		return Result.ok(sysUserSignService.querySignByUserId());
	}

	/**
	 * 修改签字密码
	 * Update password result
	 *
	 * @param data data
	 * @return the result
	 */
	@PostMapping("/updatePassword")
	public Result<?> updatePassword(@RequestBody Map<String, Object> data) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		SysUser user = sysUserService.getById(loginUser.getId());
		String password = PasswordUtil.encrypt(user.getUsername(), String.valueOf(data.get("dlPsd")), user.getSalt());
		if (user.getPassword().equals(password)) {
			SysUserSign sysUserSign = sysUserSignService.getOne(new QueryWrapper<SysUserSign>().eq("user_id", user.getId()));
			sysUserSign.setPassword(String.valueOf(data.get("newPsd")));
			sysUserSignService.updateById(sysUserSign);
			return Result.ok((Object) "修改成功");
		} else {
			return Result.error("登录密码错误!");
		}
	}

	/**
	 * 修改签字图片
	 * Update base 64 result
	 *
	 * @param data data
	 * @return the result
	 */
	@PostMapping("/updateBase64")
	public Result<?> updateBase64(@RequestBody Map<String, Object> data) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		SysUserSign sysUserSign = sysUserSignService.getOne(new QueryWrapper<SysUserSign>().eq("user_id", loginUser.getId()));
		sysUserSign.setSignBase64(String.valueOf(data.get("base64Text")));
		sysUserSignService.updateById(sysUserSign);
		return Result.ok("修改成功");
	}

	/**
	 * 更新签字密码和照片
	 * Update password result
	 *
	 * @param data data
	 * @return the result
	 */
	@PostMapping("/updateSign")
	public Result<?> updateSign(@RequestBody Map<String, Object> data) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		boolean update = true;
		SysUserSign sysUserSign = sysUserSignService.getOne(new QueryWrapper<SysUserSign>().eq("user_id", loginUser.getId()));
		if (sysUserSign == null) {
			sysUserSign = new SysUserSign();
			sysUserSign.setId(IdUtil.objectId());
			sysUserSign.setUserId(loginUser.getId());
			sysUserSign.setUserIdText(loginUser.getRealname());
			sysUserSign.setTenantId(loginUser.getTenantId());
			update = false;
		}
		sysUserSign.setSignBase64(String.valueOf(data.get("base64Text")));
		sysUserSign.setPassword(String.valueOf(data.get("password")));
		if (update) {
			sysUserSignService.updateById(sysUserSign);
		} else {
			sysUserSignService.save(sysUserSign);
		}
		return Result.ok("修改成功");
	}

	/**
	 * 验证密码
	 * Verify password result
	 *
	 * @param password password
	 * @return the result
	 */
	@PostMapping("/verifyPassword")
	public Result<?> verifyPassword(String password) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		SysUserSign sysUserSign = sysUserSignService.getOne(new QueryWrapper<SysUserSign>().eq("user_id", loginUser.getId()));
		if (sysUserSign.getPassword().equals(password)) {
			return Result.ok((Object) "签字密码正确!");
		} else {
			return Result.error("签字密码错误!");
		}
	}

	/**
	 * 获取当前用户签字信息，没有报错
	 *
	 * @return the result
	 */
	@PostMapping("/queryAppSignText")
	public Result<?> queryAppSignText() {
		return Result.ok(sysUserSignService.queryAppSignText());
	}


	/**
	 * 获取当前用户签字信息，没有也不报错
	 *
	 * @return the result
	 */
	@GetMapping("/queryUserSignText")
	public String queryUserSignText() {
		return sysUserSignService.queryUserSignText();
	}

	/**
	 * 用户list
	 * Query user list result
	 *
	 * @return the result
	 */
	@PostMapping("/queryUserList")
	public Result<?> queryUserList() {
		QueryWrapper userWrapper = new QueryWrapper();
		userWrapper.ne("is_admin", "1");
		List<SysUser> list = sysUserService.list(userWrapper);
		return Result.ok(list);
	}

}
